package com.rainliang.project.connect.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.rainliang.project.connect.bean.ConnectBean;
import com.rainliang.project.connect.dao.ConnectDao;
import com.rainliang.project.database.Db;
import com.rainliang.project.util.DateUtil;



public class ConnectDaoImpl implements ConnectDao {

	@Override
	public ConnectBean addConn(ConnectBean cb) {
		return addConnectBean(cb)>0?cb:null;
	}

	@Override
	public int addConnectBean(ConnectBean cb) {
		String sql="insert into conn_txqq (user_id,conn_id,conn_seting,conn_isbind,access_token," +
				"refresh_token,token_attach,create_time) values (?,?,?,?,?,?,?,?)";
		
		long resutl=Db.executeUpdate(sql, new Object[] {
				cb.getUserId(),cb.getConnectId(),cb.getSetting(),cb.getIsbind(),
				cb.getAccessToken(),cb.getRefreshToken(),cb.getTokenAttach(),DateUtil.getDate()});
		return Integer.parseInt(resutl+"");
	}

	@Override
	public int updateConnectToken(ConnectBean cb) {
		StringBuilder sb=new StringBuilder("update conn_txqq set access_token=?,token_attach=? where user_id=?");
		long value=Db.executeUpdate(sb.toString(),new Object[]{cb.getAccessToken(),cb.getTokenAttach(),cb.getUserId()});
		return Integer.parseInt(value+"");
	}

	@Override
	public int deleteConnectBeanById(Long userId) {
		if(userId==null)return -1;
		long value=Db.executeUpdate("delete from conn_txqq where user_id=?",new Object[]{userId});
		return Integer.parseInt(value+"");
	}

	@Override
	public ConnectBean getById(Long id) {
		// TODO Auto-generated method stub
		String sql="select * from conn_txqq where user_id=?";
		List<Map<String, Object>> result = Db.executeQuery(sql,new Object[] { id});
		if(result!=null&&result.size()>0){
			return ConnectBean.parseMap(result.get(0));
		}
		return null;
	}

	@Override
	public List<ConnectBean> getListByParam(Map<String, Object> param,String oper) {
		StringBuilder sql=new StringBuilder("select * from conn_txqq where ");
		List<Object> paramList=new ArrayList<Object>(); 
		Set<String> key = param.keySet();
        for (Iterator<String> it = key.iterator(); it.hasNext();) {
            String paramkey = (String) it.next();
            Object paramvalue=param.get(paramkey);
            if(paramkey!=null&&paramvalue!=null){
            	sql.append(" "+paramkey+"=? "+oper);
            	paramList.add(paramvalue);
            }
        }
        if(sql.lastIndexOf(oper)>0){
        	sql.delete(sql.lastIndexOf(oper),sql.length());
        }
        
        List<Map<String, Object>> result = Db.executeQuery(sql.toString(),paramList.toArray());
		return ConnectBean.parseMapList(result);
	}

	@Override
	public boolean addConnRelation(Long userId, int relation) {
		String sql="select relation from conn_relation where user_id=?";
		List<Map<String, Object>> result = Db.executeQuery(sql,new Object[]{userId});
		long f=0;
		if(result!=null&&result.size()>0){
			Map<String, Object> map=result.get(0);
			if(map!=null){
				Integer relationOld=(Integer)map.get("relation");
				relationOld+=relation;
				f=Db.executeUpdate("update conn_relation set relation=? where user_id=?",new Object[]{relationOld,userId});
			}
		}else{
			f=Db.executeUpdate("insert into conn_relation (user_id,relation,create_time) values (?,?,NOW())",
					new Object[]{userId,relation});
		}
		return f>0;
	}
	
}
